package tmproject.hlhj.fhp.anwser.http

import android.util.Log
import com.google.gson.Gson
import com.lzy.okgo.model.Response
import rx.Observable
import rx.Observer
import tmproject.hlhj.fhp.anwser.bean.BaseBean
import tmproject.hlhj.fhp.anwser.utils.LoadingUtils


/**
 * Created by Never Fear   on 2018\8\21 0021.
Never More....
 */
interface BaseAction<T> : Observer<T> {

    override fun onError(e: Throwable?) {
        Log.e("okgo",e?.toString())
        e?.printStackTrace()
        httpFail(e)
    }

    override fun onNext(t: T) {
        if (t is Response<*>){
            if (t.isSuccessful){
                Log.e("okgo","responce的Body为${t.body().toString()}")
                val str = Gson().toJson(t.body())
                val baseBean = Gson().fromJson(str, BaseBean::class.java)
                if (baseBean.code==200){
                    httpSuccess(t)
                }else{
                        httpErroCode(baseBean)
                }
            }
        }
    }
    override fun onCompleted() {
        LoadingUtils.loadingDismiss()
    }
    fun httpSuccess(t:T)
    fun httpErroCode(baseBean: BaseBean)
    fun httpFail(e:Throwable?)
}